package gui.pages;

import SqlConnect.SqlInvestment;
import gui.MainFrame;
import gui.tool.GridBagPanel;

import javax.swing.*;
import java.awt.*;

public class Page3 extends JPanel {
    public Page3() {
        super(new BorderLayout());
        GridBagPanel submitter = new GridBagPanel();
        JButton submit = new JButton("提交");
        submitter.add(submit, 0, 0, 1, 1);
        add(submitter, BorderLayout.SOUTH);
        GridBagPanel texts = new GridBagPanel();
        JTextField[] t = new JTextField[6];
        for (int i = 0; i < 6; i++) {
            texts.add(new JLabel(SqlInvestment.guiTag[i] + ":"), 0, i, 1, 1);
            t[i] = new JTextField(30);
            texts.add(t[i], 1, i, 2, 1);
        }
        add(texts, BorderLayout.CENTER);
        submit.addActionListener(event -> {
            String[] result = new String[6];
            for (int i = 0; i < 6; i++) {
                result[i] = t[i].getText().strip();
                if (result[i].equals("")) {
                    JOptionPane.showMessageDialog(null, "添加失败。\n错误：有空白", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
            if (MainFrame.sql.inv.add(result)) {
                StringBuilder message = new StringBuilder("<html><body><p>添加成功<br>详细信息:</p><table>");
                for (int i = 0; i < 6; i++) {
                    message.append("<tr><td>").append(SqlInvestment.guiTag[i]).append("</td><td>").append(result[i])
                            .append("</td></tr>");
                }
                message.append("</table></body></html>");
                JOptionPane.showMessageDialog(null, message, "添加信息", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(null, "添加失败。\n错误：项目名重复。", "错误", JOptionPane.ERROR_MESSAGE);
            }
        });
    }
}
